﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;

namespace UserInterface
{
    using BusinessLogicLayer;
    using BusinessObjectLayer;
    using System.Net;
    using System.IO;

    public partial class ShowHelpUI : Form
    {
        HelpBLL _HelpBLL;
        ExceptionHandlerBOL _ExceptionHandlerBOL;
        private AccessMode _AccessMode;
        public ShowHelpUI(AccessMode accessMode)
        {
            InitializeComponent();
            newHelpButton.Enabled = (accessMode != AccessMode.Read);
            _AccessMode = accessMode;
            _HelpBLL = new HelpBLL();
            fillDataGridView();
        }
        private void fillDataGridView()
        {
            try
            {
                string serverPath = ServerBLL.GetStringValue("ServerPath");
                string userName = ServerBLL.GetStringValue("UserName");
                string password = ServerBLL.GetStringValue("Password");
                string domain = ServerBLL.GetStringValue("Domain");
                HelpBOL[] HelpRecords = _HelpBLL.Select();
                HelpDataGridView.Rows.Clear();
                if (HelpRecords != null)
                    for (int index = 0; index < HelpRecords.Length; index++)
                    {
                        HelpDataGridView.Rows.Add();
                        DataGridViewRow row = HelpDataGridView.Rows[index];
                        row.Cells["helpId"].Value = HelpRecords[index].HelpId;
                        row.Cells["helpTitle"].Value = HelpRecords[index].HelpTitle;
                        row.Cells["description"].Value = HelpRecords[index].Description;
                        row.Cells["imageURL"].Value = HelpRecords[index].ImageURL;
                        row.Cells["fileURL"].Value = HelpRecords[index].FileURL;
                        //////DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
                        ////////ftp://31.25.89.20/mahdielab.com/wwwroot/Images/Help/
                        ////////string imagepath = string.Format("{0}/{1}/Images/Help/{2}", serverPath, domain, HelpRecords[index].ImageURL);
                        ////////FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + imagepath);
                        ////////request.Credentials = new NetworkCredential(userName, password);
                        ////////request.Method = WebRequestMethods.Ftp.DownloadFile;
                        ////////FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                        ////////StreamReader reader = new StreamReader(response.GetResponseStream());
                        ////////string content = reader.ReadToEnd();
                        ////////Image image = Image.FromFile("");
                        ////////imageColumn.Image = image;
                        ////////HelpDataGridView.Columns.Add(imageColumn);
                        ////////reader.Close();
                        ////////response.Close(); 
                    }
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                messageTitleLabel.ForeColor = Color.Red;
                messageTitleLabel.Text = _ExceptionHandlerBOL.Title;
            }
        }
        private void HelpDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                messageTitleLabel.Text = string.Empty;
                if (e.RowIndex != -1 && _AccessMode == AccessMode.Complete)
                    ItemSelected(e.RowIndex);
                if (_AccessMode != AccessMode.Complete)
                {
                    _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(new Exception("NoAccess"));
                    messageTitleLabel.ForeColor = Color.Red;
                    messageTitleLabel.Text = _ExceptionHandlerBOL.Title;
                }
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                messageTitleLabel.ForeColor = Color.Red;
                messageTitleLabel.Text = _ExceptionHandlerBOL.Title;
            }
        }
        private void ItemSelected(int selectedIndex)
        {
            try
            {
                short helpId = Convert.ToInt16(HelpDataGridView.Rows[selectedIndex].Cells["helpId"].Value);
                string helpTitle = (HelpDataGridView.Rows[selectedIndex].Cells["helpTitle"].Value == null ? null : HelpDataGridView.Rows[selectedIndex].Cells["helpTitle"].Value.ToString());
                string description = (HelpDataGridView.Rows[selectedIndex].Cells["description"].Value == null ? null : HelpDataGridView.Rows[selectedIndex].Cells["description"].Value.ToString());
                string imageURL = (HelpDataGridView.Rows[selectedIndex].Cells["imageURL"].Value == null ? null : HelpDataGridView.Rows[selectedIndex].Cells["imageURL"].Value.ToString());
                string fileURL = (HelpDataGridView.Rows[selectedIndex].Cells["fileURL"].Value == null ? null : HelpDataGridView.Rows[selectedIndex].Cells["fileURL"].Value.ToString());
                HelpBOL _HelpBOL = new HelpBOL(helpId, helpTitle, description, fileURL, imageURL);
                new EditHelpUI(_HelpBOL).ShowDialog();
                fillDataGridView();
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                messageTitleLabel.ForeColor = Color.Red;
                messageTitleLabel.Text = _ExceptionHandlerBOL.Title;
            }
        }
        private void HelpdataGridView_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyValue == '\r')
                    if (HelpDataGridView.Rows.Count != 0)
                        ItemSelected(HelpDataGridView.CurrentRow.Index);
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                messageTitleLabel.ForeColor = Color.Red;
                messageTitleLabel.Text = _ExceptionHandlerBOL.Title;
            }
        }
        private void closeForm()
        {
            this.Close();
        }
        private void newHelpButton_Click(object sender, EventArgs e)
        {
            try
            {
                new EditHelpUI().ShowDialog();
                fillDataGridView();
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                messageTitleLabel.ForeColor = Color.Red;
                messageTitleLabel.Text = _ExceptionHandlerBOL.Title;
            }
        }
        private void ShowHelp_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                closeForm();
        }
        private void messageTitleLabel_Click(object sender, EventArgs e)
        {
            if (_ExceptionHandlerBOL != null)
                new ExceptionHandlerUI(_ExceptionHandlerBOL).ShowDialog();
            _ExceptionHandlerBOL = null;
        }
    }
}